<?php

abstract class WeModuleSite {

    public $modulename;
    public function __call($name, $arguments) {
        global $_W;
        load()->func('pdo');
        $base_url =$_W['siteroot'];
        $store_url="index.php?s=/store";
        $this->modulename = $_SESSION['modulename'];
        $isWeb = stripos($name, 'doWeb') === 0;
        $isMobile = stripos($name, 'doMobile') === 0;
        $isApi = stripos($name, 'doApi') === 0;

        if($isWeb || $isMobile||$isApi) {
            $dir = IA_ROOT . '/addons/' . $this->modulename . '/inc/';
            if($isWeb) {
                $dir .= 'web/';
                $fun = strtolower(substr($name, 5));
            }
            if($isMobile) {
                $dir .= 'mobile/';
                $fun = strtolower(substr($name, 8));
            }
            if($isApi) {
                $dir .= 'api/';
                $fun = strtolower(substr($name, 5));
            }
            //var_dump($fun);die();
            $file = $dir . $fun . '.inc.php';
            if(file_exists($file)) {
                require $file;
                exit;
            } else {
                $dir = str_replace("addons", "framework/builtin", $dir);
                $file = $dir . $fun . '.inc.php';
                if(file_exists($file)) {
                    require $file;
                    exit;
                }
            }
        }
        trigger_error("访问的方法 {$name} 不存在.", E_USER_WARNING);
        return null;
    }

    public function __construct(){
        global $_W;
      //var_dump($_W);die();



    }
    public function template($filename, $flag = TEMPLATE_DISPLAY)
    {
        global $_W;

        if($filename=='common/header'||$filename=='common/footer'||$filename=='common/tpl/file_image'){
            $source = IA_ROOT . "/addons/{$_W['modules']['identifie']}/template/{$filename}.html";
            if(!is_file($source)){
                $source = IA_ROOT . "/web/themes/web/{$filename}.html";
            }

        }else{
            $source = IA_ROOT . "/addons/{$_W['modules']['identifie']}/template/{$filename}.html";
        }
        $compile = IA_ROOT . "/runtime/tpl/web/{$_W['modules']['identifie']}/default/{$filename}.tpl.php";

        if (!is_file($source)) {
            exit("we_Error: template source '{$filename}' is not exist!");
        }
        if (!is_file($compile) || filemtime($source) > filemtime($compile)) {
            $this->template_compile($source, $compile);
        }
        switch ($flag) {
            case TEMPLATE_DISPLAY:
            default:
                extract($GLOBALS, 1);
                return $compile;
                break;
            case TEMPLATE_FETCH:
                extract($GLOBALS, 1);
                ob_flush();
                ob_clean();
                ob_start();
                include $compile;
                $contents = ob_get_contents();
                ob_clean();
                return $contents;
                break;
            case TEMPLATE_INCLUDEPATH:
                return $compile;
                break;
        }
    }


    public function template_compile($from, $to, $inmodule = false)
    {
        $path = dirname($to);
        if (!is_dir($path)) {
            load()->func('file');
            mkdirs($path);
        }
        $content = $this->template_parse(file_get_contents($from), $inmodule);
        if (IMS_FAMILY == 'x' && !preg_match('/(footer|header|account\/welcome|login|register)+/', $from)) {
            $content = str_replace('餐饮', '系统', $content);
        }
        file_put_contents($to, $content);
    }


    public function template_parse($str, $inmodule = false)
    {
        $str = preg_replace('/<!--{(.+?)}-->/s', '{$1}', $str);
        $str = preg_replace('/{template\s+(.+?)}/', '<?php (!empty($this) && $this instanceof WeModuleSite || ' . intval($inmodule) . ') ? (include $this->template($1, TEMPLATE_INCLUDEPATH)) : (include template($1, TEMPLATE_INCLUDEPATH));?>', $str);
        $str = preg_replace('/{php\s+(.+?)}/', '<?php $1?>', $str);
        $str = preg_replace('/{if\s+(.+?)}/', '<?php if($1) { ?>', $str);
        $str = preg_replace('/{else}/', '<?php } else { ?>', $str);
        $str = preg_replace('/{else ?if\s+(.+?)}/', '<?php } else if($1) { ?>', $str);
        $str = preg_replace('/{\/if}/', '<?php } ?>', $str);
        $str = preg_replace('/{loop\s+(\S+)\s+(\S+)}/', '<?php if(is_array($1)) { foreach($1 as $2) { ?>', $str);
        $str = preg_replace('/{loop\s+(\S+)\s+(\S+)\s+(\S+)}/', '<?php if(is_array($1)) { foreach($1 as $2 => $3) { ?>', $str);
        $str = preg_replace('/{\/loop}/', '<?php } } ?>', $str);
        $str = preg_replace('/{(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)}/', '<?php echo $1;?>', $str);
        $str = preg_replace('/{(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\[\]\'\"\$]*)}/', '<?php echo $1;?>', $str);
        $str = preg_replace('/{url\s+(\S+)}/', '<?php echo url($1);?>', $str);
        $str = preg_replace('/{url\s+(\S+)\s+(array\(.+?\))}/', '<?php echo url($1, $2);?>', $str);
        $str = preg_replace('/{media\s+(\S+)}/', '<?php echo tomedia($1);?>', $str);
        $str = preg_replace_callback('/<\?php([^\?]+)\?>/s', "template_addquote", $str);
        $str = preg_replace('/{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)}/s', '<?php echo $1;?>', $str);
        $str = str_replace('{##', '{', $str);
        $str = str_replace('##}', '}', $str);
        if (!empty($GLOBALS['_W']['setting']['remote']['type'])) {
            $str = str_replace('</body>', "<script>$(function(){\$('img').attr('onerror', '').on('error', function(){if (!\$(this).data('check-src') && (this.src.indexOf('http://') > -1 || this.src.indexOf('https://') > -1)) {this.src = this.src.indexOf('{$GLOBALS['_W']['attachurl_local']}') == -1 ? this.src.replace('{$GLOBALS['_W']['attachurl_remote']}', '{$GLOBALS['_W']['attachurl_local']}') : this.src.replace('{$GLOBALS['_W']['attachurl_local']}', '{$GLOBALS['_W']['attachurl_remote']}');\$(this).data('check-src', true);}});});</script></body>", $str);
        }
        $str = "<?php defined('IN_IA') or exit('Access Denied');?>" . $str;
        return $str;
    }

    public function createWebUrl($do,$params=array()){
         global $_W;
         $param = array();
         $param['module'] = $_SESSION['modulename'];
        // var_dump($params);die();
         if(!empty($do)){
            $param['do'] = $do;
         }
         $arr = array_merge($params, $param);

          return  $_W['siteroot'].'/'.url('run',$arr);
    }

}


?>